summaryrefslogtreecommitdiffstats
path: root/src/core/file_sys/fs_memory_management.h
blob: f03c6354b81681fb9a03df3541a33e9f2c2e7964 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// SPDX-FileCopyrightText: Copyright 2024 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#pragma once

#include <mutex>
#include "common/alignment.h"

namespace FileSys {

constexpr size_t RequiredAlignment = alignof(u64);

void* AllocateUnsafe(size_t size) {
    // Allocate
    void* const ptr = ::operator new(size, std::align_val_t{RequiredAlignment});

    // Check alignment
    ASSERT(Common::IsAligned(reinterpret_cast<uintptr_t>(ptr), RequiredAlignment));

    // Return allocated pointer
    return ptr;
}

void DeallocateUnsafe(void* ptr, size_t size) {
    // Deallocate the pointer
    ::operator delete(ptr, std::align_val_t{RequiredAlignment});
}

void* Allocate(size_t size) {
    return AllocateUnsafe(size);
}

void Deallocate(void* ptr, size_t size) {
    // If the pointer is non-null, deallocate it
    if (ptr != nullptr) {
        DeallocateUnsafe(ptr, size);
    }
}

} // namespace FileSys